﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;

namespace jSLCore.Client.Presentation.TelerikSupport.Controls
{
    public partial class jStringFilteringControl : UserControl , IFilteringControl
    {
        public jStringFilteringControl()
        {
            InitializeComponent();
        }

        private TextFilterDescriptor _descriptor;
        private GridViewBoundColumnBase _column;
        public  void Prepare(GridViewBoundColumnBase column)
        {
            _column = column;
            txt.Focus();
        }

        public void Prepare(Telerik.Windows.Controls.GridViewColumn column)
        {
            if (! (column is GridViewBoundColumnBase))throw new InvalidOperationException("Column for filtering is not GridViewBoundColumnBase");
            _column = (GridViewBoundColumnBase) column;
            txt.Focus();
        }

        public bool IsActive
        {
            get { return (bool) GetValue(IsActiveProperty); }
            set
            {
                SetValue(IsActiveProperty,value);
            }
        }

        public bool CaseSensitive { get; set; }

        public static DependencyProperty IsActiveProperty = DependencyProperty.Register("IsActive", typeof (bool),
                                                                                 typeof (jStringFilteringControl), null);

        private DispatcherTimer timer;

        private void text_Changed(object sender, TextChangedEventArgs e)
        {
            if (timer != null)
            {
                timer.Stop();
                timer.Tick -= timer_Tick;
                timer = null;
            }

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += timer_Tick;
            timer.Start();
            PreSearchAnimation.Stop();
            PreSearchAnimation.Begin();
        }

        public Storyboard PreSearchAnimation
        {
            get { return (Storyboard) Resources["PreSearchAnimation"]; }
        }

        private string Text
        {
            get { return txt.Text; }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            PreSearchAnimation.Stop();
            timer.Stop();
            if(string.IsNullOrEmpty(Text))
            {
                RemoveFilter();
            }
            else
            {
                Filter();
            }
        }

        private void Filter()
        {
            ClearFilter();

            var memberPath = _column.DataMemberBinding.Path.Path;
            {
                _descriptor = new TextFilterDescriptor(memberPath,Text,CaseSensitive);
                _column.DataControl.FilterDescriptors.Add(_descriptor);
                IsActive = true;
            }
        }

        private void RemoveFilter()
        {
            ClearFilter();
            IsActive = false;
        }

        private void ClearFilter()
        {
            if(_column.DataControl.FilterDescriptors.Contains(_descriptor))
            {
                _column.DataControl.FilterDescriptors.Remove(_descriptor);
            }
        }
    }
}
